﻿using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace Keep.API.Appliction.Command.Select
{
	/// <summary>
	/// 查询产品
	/// </summary>
	public class ProductSearchSelectHandler:IRequestHandler<ProductSearchSelectCommon,Result>
	{
		private readonly IBaseRepositorys<Product> productRepository;

		public ProductSearchSelectHandler(IBaseRepositorys<Product> productRepository)
		{
			this.productRepository = productRepository;
		}
		/// <summary>
		/// 查询产品
		/// </summary>
		/// <param name="request"></param>
		/// <param name="cancellationToken"></param>
		/// <returns></returns>
		/// <exception cref="NotImplementedException"></exception>
		public Task<Result> Handle(ProductSearchSelectCommon request, CancellationToken cancellationToken)
		{
			var result = new Result() { Code=(int)CodeEnum.success,Messege="查询成功"};
			try
			{
				var list = productRepository.GetAll().Where(c => !c.DeleteDate);
				if (request.ProductId != 0)
				{
					list = list.Where(c => c.ProductId == request.ProductId);
				}
				result.Data = list.ToList();
			}
			catch (Exception)
			{

				throw;
			}

			return Task.FromResult(result);
		}
	}
}
